'use strict';
const Controller = require('egg').Controller;
class AdminController extends Controller {
  // 获取自己用户信息
  /**
       * @api {Get} /api/admin/adminInfo 获取自己商家信息
       * @apiGroup 用户/商家
       * @apiParam {String}  authorization ⚡登录成功后的token值
       * @apiParamExample 参数模板
        {
        "authorization":"123456789abcdefqwerxyzbluejgoods"
        }
       * @apiSuccessExample  成功返回
        {
        "msg": "成功操作",
        "result": {
                    "id": 1,                                    //商家id
                    "token": "123456789abcdefqwerxyzbluejgoods",//token
                    "project_id": 1,                            //项目id
                    "project_name": null,
                    "token_out_of_date": "1630117434697",
                    "type": "2",                                
                    "real_name": null,
                    "name": "jack",                             //昵称
                    "sex": null,
                    "phone": "12345678912",                     //电话
                    "password": "123456",                       //密码
                    "birth_date": null,
                    "header_img": null,
                    "openid": null,
                    "wx_name": null,
                    "total_score": 0,
                    "remain_score": 0,
                    "use_score": 0,
            }
        }
       * @apiErrorExample  错误返回
       * {
       *   "msg": "找不到该用户信息"
       * }
       */

  async adminInfo() {
    const { ctx } = this;
    ctx.body = await this.service.com.admin.adminInfo();
  }
  // 获取商家列表信息
  async index() {
    const { ctx } = this;
    ctx.body = await this.service.com.admin.index();
  }
  // 获取商家单个信息
  /**
       * @api {Get} /api/admin/:id 获取商家单个信息
       * @apiGroup 用户/商家
       * @apiParam {String} id  ⚡商家id
       * @apiParam {String} authorization ⚡登录后的token值
       * @apiParamExample 参数模板
        {
            "authorization":"123456789abcdefqwerxyzbluejgoods"
        }
       * @apiSuccessExample  成功返回
        {
            "msg": "成功操作",
            "result": {
                    "id": 1,                                    //商家id
                    "token": "123456789abcdefqwerxyzbluejgoods",//token
                    "project_id": 1,                            //项目id
                    "project_name": null,
                    "token_out_of_date": "1630117434697",
                    "type": "2",
                    "real_name": null,
                    "name": "jack",                             //昵称
                    "sex": null,
                    "phone": "12345678912",                     //电话
                    "password": "123456",                       //密码
                    "birth_date": null,
                    "header_img": null,
                    "openid": null,
                    "wx_name": null,
                    "total_score": 0,
                    "remain_score": 0,
                    "use_score": 0,
            }
        }
       * @apiErrorExample  错误返回
       * {
       *   "msg": "找不到该用户信息"
       * }
       */

  async show() {
    const { ctx } = this;

    ctx.body = await this.service.com.admin.show(ctx.params.id);
  }
  // 更新商家信息
  /**
       * @api {put} /api/admin/:id 更新商家信息(管理台)
       * @apiGroup 用户/商家
       * @apiParam {String} authorization ⚡登录后的token值
       * @apiParam {String}  name        ⚡昵称    
       * @apiParam {String}  phone       ⚡手机号码
       * @apiParam {String}  password    ⚡密码    
       * @apiParam {String}  verify        验证码
       * @apiParam {String}  real_name   真实名称
       * @apiParam {INTEGER} birth_date  出生日期
       * @apiParam {String}  password    密码
       * @apiParam {String}  header_img  头像
       * @apiParamExample 参数模板
        {
            "authorization":"123456789abcdefqwerxyzbluejgoods"
            "name":"jack",
            "phone":"12345678912",
            "password":"654321"
        }
       * @apiSuccessExample  成功返回
        {
        "code": 1,
        "msg": "成功操作",
        "result": {
                    "id": 1,                                    //商家id
                    "token": "123456789abcdefqwerxyzbluejgoods",//token
                    "project_id": 1,                            //项目id
                    "project_name": null,
                    "token_out_of_date": "1630117434697",
                    "type": "2",
                    "real_name": null,
                    "name": "jack",                             //昵称
                    "sex": null,
                    "phone": "12345678912",                     //电话
                    "password": "654321",                       //密码
                    "birth_date": null,
                    "header_img": null,
                    "openid": null,
                    "wx_name": null,
                    "total_score": 0,
                    "remain_score": 0,
                    "use_score": 0,
            }
        }
       * @apiErrorExample  错误返回
       * {
       *   "msg": "找不到该用户信息"
       * }
       */

  async update() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;

    if (
      new ctx.helper.checkData(this, 'name', 'password', 'phone').checkDataRes
    ) {
      // dataObj.sex && !(new ctx.helper.checkData(this, 'sex')).checkDataRes;
      const id = ctx.params.id;
      ctx.body = await service.com.admin.update(id, dataObj);
    }
  }

  /**
       * @api {Post} /api/registerAdmin 商家注册
       * @apiGroup 用户/商家
       * @apiParam {String}  name        ⚡昵称    
       * @apiParam {String}  phone       ⚡手机号码
       * @apiParam {String}  password    ⚡密码 
       * @apiParam {String}  type        ⚡类型 2
       * @apiParam {String}  verify      验证码
       * @apiParam {String}  real_name   真实名称
       * @apiParam {INTEGER} birth_date  出生日期
       * @apiParam {String}  header_img  头像
       * @apiParamExample 参数模板
        {
        "name": "jack",
        "phone":"12345678912",
        "password": "123456",
        "type": "2",
        }
       * @apiSuccessExample 成功返回
       * {
       *    "msg":'成功注册'
       * }
       *
       * @apiErrorExample 错误返回
       * {
       *  "msg":"手机号码已注册"
       * }
       * @apiErrorExample 错误返回
       * {
       *  "msg":"注册失败"
       * }
       *
       */
  async create() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;
    if (new ctx.helper.checkData(this, 'password', 'phone').checkDataRes) {
      ctx.body = await service.com.admin.create(dataObj);
    }
  }

  async loginAdmin() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;
    if (new ctx.helper.checkData(this, 'phone', 'password').checkDataRes) {
      ctx.body = await service.com.admin.loginAdmin(dataObj);
    }
  }
}

module.exports = AdminController;
